#################################################################################################
#This file conducts the appendix analysis on the litigation and credit claiming paper.
#################################################################################################
#set working directory
setwd("~/Dropbox/LobbyingInsideOut/LitigationPaper/JLC replication")

#clear working environment
rm(list=ls()) 

#load packages  
library(tidyverse)
library(ggplot2)
library(readxl)
library(data.table)
library(rstatix)
library(xtable)

##################################################################
# Read in data
##################################################################
federal_orgs <-  read.csv("./data/federally_focused_orgs.csv") %>%
  mutate(federal=1,facebook.handle.final=ifelse(facebook.handle.final=="na",NA,facebook.handle.final)) %>%
  filter(!is.na(facebook.handle.final)) %>% distinct() %>%
  dplyr::mutate(twitter.handle.final = tolower(twitter.handle.final), twitter.handle.final = str_replace(twitter.handle.final, "\\@", ""),
                twitter.handle.final = str_trim(twitter.handle.final),
                Organization = gsub("&", "and", organizationNames), Organization = tolower(Organization),
                facebook.handle.final=ifelse(facebook.handle.final=="",NA,facebook.handle.final))
amicus_orgs <-  fread("./data/AmiciOrgsClean.csv") %>% 
  filter(use==1) %>% mutate(amicus=1) %>% dplyr::mutate(twitter.handle.final = tolower(twitter.handle), twitter.handle.final = str_replace(twitter.handle.final, "\\@", ""),
                                                        twitter.handle.final = str_trim(twitter.handle.final),
                                                        Organization = gsub("&", "and", organizationNames), Organization = tolower(Organization),
                                                        facebook.handle.final=ifelse(facebook.handle.final=="",NA,facebook.handle.final)) %>%
  distinct() %>%  filter(!is.na(facebook.handle.final)) 
all_orgs <- full_join(federal_orgs,amicus_orgs,by=c("facebook.handle.final"="facebook.handle.final",
                                                    "twitter.handle.final"="twitter.handle.final",
                                                    "Type"="Type","LegalOrg"="LegalOrg")) %>%
  mutate(og_only=ifelse(federal==1 & is.na(amicus),1,0),amicus = ifelse(is.na(amicus),0,amicus))  %>%
  select(Type, amicus, og_only, twitter.handle.final, LegalOrg,facebook.handle.final) %>% 
  group_by(facebook.handle.final,twitter.handle.final) %>% fill(everything(),.direction="updown") %>% 
  distinct() %>% filter(Type!= "Think Tank") %>% filter(facebook.handle.final != "0")

ct_amicus_orgs_full <- fread("./data/ct_amicus_orgs_full_JLC.csv")
ct_federal_orgs_full <- fread("./data/ct_federal_orgs_full_JLC.csv")
twitter_federal_orgs_full <- fread("./data/twitter_federal_orgs_full_JLC.csv")
ct_fuzzy <- fread("./data/ct_fuzzyjoin_scotus_full_JLC.csv")
twitter_fuzzy <- fread("./data/twitter_fuzzyjoin_scotus_full_JLC.csv")
ct_amicus_orgs_dictionary <- fread("./data/ct_amicus_orgs_dictionary_full_JLC.csv")
ct_federal_orgs_dictionary <- fread("./data/ct_federal_orgs_dictionary_full_JLC.csv") 
twitter_amicus_orgs_dictionary <- fread("./data/twitter_amicus_orgs_dictionary_JLC.csv")
twitter_federal_orgs_dictionary <- fread("./data/twitter_federal_orgs_dictionary_JLC.csv")

#################################################################
# Tables
##################################################################
#Table 1: Courts_Top20
facebookcourts <- full_join(ct_amicus_orgs_dictionary %>% select(Type.y,Post.Created,id,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                              dplyr::rename(Type=Type.y) %>% mutate(Post.Created=as.Date(Post.Created)),
                            ct_federal_orgs_dictionary %>% select(Type.x,Created,id,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                              dplyr::rename(Type=Type.x,Post.Created=Created) %>% mutate(Post.Created=as.Date(Post.Created))) %>%
  distinct() %>%
  filter(court_words==1) %>% filter(courts_to_remove==0) %>%
  mutate(Post="Courts",Platform="Facebook") %>%
  filter(Type!="")

tweetscourts <- full_join(twitter_amicus_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove),
                          twitter_federal_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                            dplyr::rename(Type=Type,LegalOrg=LegalOrg)) %>%
  distinct() %>%
  filter(court_words==1) %>% filter(courts_to_remove==0) %>%
  mutate(Post="Courts",Platform="Twitter") %>%
  filter(Type!="")

allcourts <- cbind(facebookcourts %>%
                     group_by(Page.Name,Type) %>%
                     dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Facebook Page Name`=Page.Name)%>%
                     as.data.frame()%>%
                     slice(1:20),tweetscourts %>%
                     group_by(handle,Type) %>%
                     dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Twitter Handle`=handle)%>%
                     as.data.frame()%>%
                     slice(1:20)) 

print(xtable(allcourts,caption="The top 20 organizations who posted about the courts on Facebook (left) and Twitter (right), as well as their type and the number of posts.",
             label="Courts_top20"),file="Tables/Courts_Top20.tex",size="scriptsize")

#Table 2: Amicus_Top20
facebookamicus <- full_join(ct_amicus_orgs_dictionary %>% select(id,Type.y,Post.Created,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                              dplyr::rename(Type=Type.y) %>% mutate(Post.Created=as.Date(Post.Created)),
                            ct_federal_orgs_dictionary %>% select(id,Type.x,Created,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                              dplyr::rename(Type=Type.x,Post.Created=Created) %>% mutate(Post.Created=as.Date(Post.Created))) %>%
  distinct() %>% 
  filter(amicus_words==1) %>% filter(amicus_to_remove==0) %>%
  mutate(Post="Amicus",Platform="Facebook") %>%
  filter(Type!="")
tweetsamicus <- full_join(twitter_amicus_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove),
                          twitter_federal_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove) %>%
                            dplyr::rename(Type=Type,LegalOrg=LegalOrg)) %>%
  distinct() %>% 
  filter(amicus_words==1) %>% filter(amicus_to_remove==0) %>%
  mutate(Post="Amicus",Platform="Twitter") %>%
  filter(Type!="") 
allamicus <- cbind(facebookamicus %>%
                     group_by(Page.Name,Type) %>%
                     dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Facebook Page Name`=Page.Name)%>%
                     as.data.frame()%>%
                     slice(1:20),tweetsamicus %>%
                     group_by(handle,Type) %>%
                     dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Twitter Handle`=handle)%>%
                     as.data.frame()%>%
                     slice(1:20))

print(xtable(allamicus,caption="The top 20 organizations who posted about amicus briefs on Facebook (left) and Twitter (right), as well as their type and the number of posts.",
             label="Amicus_top20"),file="Tables/Amicus_Top20.tex",size="scriptsize")

#Table 3 top case names
facebookcase <- full_join(ct_amicus_orgs_dictionary %>% select(id,Type.y,Post.Created,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                            dplyr::rename(Type=Type.y) %>% mutate(Post.Created=as.Date(Post.Created)),
                          ct_federal_orgs_dictionary %>% select(id,Type.x,Created,Page.Name,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove,courts_to_remove) %>%
                            dplyr::rename(Type=Type.x,Post.Created=Created) %>% mutate(Post.Created=as.Date(Post.Created))) %>%
  distinct() %>% 
  filter(case_words==1) %>% filter(amicus_to_remove==0) %>%
  mutate(Post="Case Words",Platform="Facebook") %>%
  filter(Type!="")
tweetscase <- full_join(twitter_amicus_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove),
                        twitter_federal_orgs_dictionary %>% select(Type,created_at,tweet_id,handle,LegalOrg,og_only,court_words,amicus_words,case_words,amicus_to_remove) %>%
                          dplyr::rename(Type=Type,LegalOrg=LegalOrg)) %>%
  distinct() %>% 
  filter(case_words==1) %>% filter(amicus_to_remove==0) %>%
  mutate(Post="Case Words",Platform="Twitter") %>%
  filter(Type!="") 
allcase <- cbind(facebookcase %>%
                   group_by(Page.Name,Type) %>%
                   dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Facebook Page Name`=Page.Name)%>%
                   as.data.frame()%>%
                   slice(1:20),tweetscase %>%
                   group_by(handle,Type) %>%
                   dplyr::summarise(Posts=n()) %>% arrange(desc(Posts)) %>% dplyr::rename(`Twitter Handle`=handle)%>%
                   as.data.frame()%>%
                   slice(1:20))

print(xtable(allcase,caption="The top 20 organizations who posted about Supreme Court cases on Facebook (left) and Twitter (right), as well as their type and the number of posts.",
             label="Cases_top20"),file="Tables/Cases_Top20.tex",size="scriptsize")

#Table 4: ttestgroupsweeklyavgpostsbygroup
stat.test <- ct_federal_orgs_full %>% filter(Type!="") %>%
  t_test(court_words ~ Type,detailed=T) %>%
  adjust_pvalue(method = "bonferroni") %>%
  select(-estimate,-.y.,-p:-alternative,-p.adj.signif) %>%
  dplyr::rename(`Mean of Group 1`=estimate1,`Mean of Group 2`=estimate2,`Group 1`=group1,
                `Group 2`=group2,`Group 1 N`=n1,`Group 2 N`=n2,`T-Statistic`=statistic,`Adjusted P-Value`=p.adj)

print(xtable(stat.test,caption="Results from pairwise t-tests, comparing the proportion of posts related to court terms by group type on Facebook. P-values of the indicated comparison are included (i.e., whether the difference is statistically significant) and use a Bonferroni correction.",
             label="ttestgroupsweeklyavgpostsbygroup",digits=c(0,4,4,0,0,0,0,4,-5)), size="\\scriptsize",file="Tables/ttestgroupsweeklyavgpostsbygroup.tex",include.rownames=F)

#Table 5: ttestgroupsweeklyavgpostsbygrouptwitter
stat.test.twitter <- twitter_federal_orgs_full %>% filter(Type!="") %>%
  t_test(court_words ~ Type,detailed=T) %>%
  adjust_pvalue(method = "bonferroni") %>%
  select(-estimate,-.y.,-p:-alternative,-p.adj.signif) %>%
  dplyr::rename(`Mean of Group 1`=estimate1,`Mean of Group 2`=estimate2,`Group 1`=group1,
                `Group 2`=group2,`Group 1 N`=n1,`Group 2 N`=n2,`T-Statistic`=statistic,`Adjusted P-Value`=p.adj)

print(xtable(stat.test.twitter,caption="Results from pairwise t-tests, comparing the proportion of posts related to court terms on Twitter. P-values of the indicated comparison are included (i.e., whether the difference is statistically significant) and use a Bonferroni correction.",
             label="ttestgroupsweeklyavgpostsbygrouptwitter",digits=c(0,4,4,0,0,0,0,4,-5)), size="\\scriptsize",file="Tables/ttestgroupsweeklyavgpostsbygrouptwitter.tex",include.rownames=F)

#Table 6: ttestgroupsall
stat.test <- full_join(ct_amicus_orgs_dictionary %>% select(-Likes.at.Posting,-Followers.at.Posting), 
                       ct_federal_orgs_dictionary) %>%
  distinct() %>%
  mutate(Group=ifelse(LegalOrg==1 & og_only==0,"Legal Amicus",
                      ifelse(LegalOrg==1 & og_only==1,"Legal Non-Amicus",
                             ifelse(LegalOrg==0 & og_only==1,"Non-Legal Non-Amicus",
                                    ifelse(LegalOrg==0 & og_only==0,"Non-Legal Amicus",NA))))) %>% 
  t_test(amicus_words ~ Group,detailed=T) %>%
  adjust_pvalue(method = "bonferroni") %>%
  select(-estimate,-.y.,-p:-alternative,-p.adj.signif) %>%
  dplyr::rename(`Mean of Group 1`=estimate1,`Mean of Group 2`=estimate2,`Group 1`=group1,
                `Group 2`=group2,`Group 1 N`=n1,`Group 2 N`=n2,`T-Statistic`=statistic,`Adjusted P-Value`=p.adj)

print(xtable(stat.test,caption="Results from pairwise t-tests, comparing the proportion of posts related to amicus terms on Facebook by group type. P-values of the indicated comparison are included (i.e., whether the difference is statistically significant) and use a Bonferroni correction.",
             label="ttestgroupsall",digits=c(0,2,2,0,0,0,0,2,-5)), size="\\scriptsize",file="Tables/ttestgroupsall.tex",include.rownames=F)

#Table 7: ttestgroupsall_twit
stat.test <- tweetscourts %>% 
  mutate(Group=ifelse(LegalOrg==1 & og_only==0,"Legal Amicus",
                      ifelse(LegalOrg==1 & og_only==1,"Legal Non-Amicus",
                             ifelse(LegalOrg==0 & og_only==1,"Non-Legal Non-Amicus",
                                    ifelse(LegalOrg==0 & og_only==0,"Non-Legal Amicus",NA))))) %>%
  t_test(amicus_words ~ Group,detailed=T) %>%
  adjust_pvalue(method = "bonferroni") %>%
  select(-estimate,-.y.,-p:-alternative,-p.adj.signif) %>%
  dplyr::rename(`Mean of Group 1`=estimate1,`Mean of Group 2`=estimate2,`Group 1`=group1,
                `Group 2`=group2,`Group 1 N`=n1,`Group 2 N`=n2,`T-Statistic`=statistic,`Adjusted P-Value`=p.adj)
print(xtable(stat.test,caption="Results from pairwise t-tests, comparing the proportion of posts related to amicus terms on Twitter by group type. P-values of the indicated comparison are included (i.e., whether the difference is statistically significant) and use a Bonferroni correction.",
             label="ttestgroupsall_twit",digits=c(0,2,2,0,0,0,0,2,-5)), size="\\scriptsize",file="Tables/ttestgroupsall_twit.tex",include.rownames=F)

##################################################################
# Figures
##################################################################
#Figure 2: differencedaysdecisionwinnersonly and differencedaysdecisionwinnersonly_twit
time <- ct_fuzzy %>% 
  left_join(.,read.csv("./Data/AmiciOrgsPairs.csv"),by=c("docket.x"="docket","User.Name"="facebook.handle.final")) %>%
  mutate(Post.Created = as.Date(Post.Created),
         `Cert Date` = as.numeric(as.Date(as.character(Post.Created), format="%Y-%m-%d")-as.Date(as.character(certdate), format="%Y-%m-%d")),
         `Argument Date` = as.numeric(as.Date(as.character(Post.Created), format="%Y-%m-%d")-as.Date(as.character(argued), format="%Y-%m-%d")),
         `Decided Date` = as.numeric(as.Date(as.character(Post.Created), format="%Y-%m-%d")-as.Date(as.character(opinion), format="%Y-%m-%d")))
data_long_winners <- gather(time, `Difference From`, difference, `Cert Date`:`Decided Date`, factor_key=TRUE) %>%
  filter(direction==winner)
ggplot(data_long_winners,aes(x=difference,color=`Difference From`,linetype=`Difference From`)) +
  geom_density(size=1.2) + geom_vline(xintercept=0, linetype="dotted")+theme_bw()+
  # ggtitle("Difference in Days from Post to Days of \n Different Court Decisions")+
  ylab("Density")+xlab("Difference (Days)")+scale_color_grey(start=.1,end=.8)+ylim(0,0.01)+
  ggtitle("Facebook")+xlim(c(-500,500))+
  scale_linetype_manual(values=c("Cert Date"="dashed","Argument Date"="twodash","Decided Date"="dotted"))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))
ggsave("./Figures/differencedaysdecisionwinnersonly.pdf",height=6, width=8)

time_twitter <- twitter_fuzzy %>% 
  left_join(.,read.csv("./Data/AmiciOrgsPairs.csv"),by=c("docket.x"="docket","handle"="twitter.handle")) %>%
  mutate(created_at = as.Date(created_at),
         `Cert Date` = as.numeric(as.Date(as.character(created_at), format="%Y-%m-%d")-as.Date(as.character(certdate), format="%Y-%m-%d")),
         `Argument Date` = as.numeric(as.Date(as.character(created_at), format="%Y-%m-%d")-as.Date(as.character(argued), format="%Y-%m-%d")),
         `Decided Date` = as.numeric(as.Date(as.character(created_at), format="%Y-%m-%d")-as.Date(as.character(opinion), format="%Y-%m-%d")))

data_long_twitter_winners <- gather(time_twitter, `Difference From`, difference, `Cert Date`:`Decided Date`, factor_key=TRUE) %>% 
  filter(direction==winner)
ggplot(data_long_twitter_winners,aes(x=difference,color=`Difference From`,linetype=`Difference From`)) +
  geom_density(size=1.2) + geom_vline(xintercept=0, linetype="dotted")+theme_bw()+
  # ggtitle("Difference in Days from Post to Days of \n Different Court Decisions")+
  ylab("Density")+xlab("Difference (Days)")+scale_color_grey(start=.1,end=.9)+ylim(0,0.01)+xlim(c(-500,500))+
  scale_linetype_manual(values=c("Cert Date"="dashed","Argument Date"="twodash","Decided Date"="dotted"))+
  ggtitle("Twitter")+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), 
                           axis.line = element_line(colour = "black"))
ggsave("./Figures/differencedaysdecisionwinnersonly_twit.pdf",height=6, width=8)


